<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社会信用代码" prop="creditCode" style="display: none;">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入社会信用代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司性质" prop="companyNature"  style="display: none;">
        <el-input
          v-model="queryParams.companyNature"
          placeholder="请输入公司性质"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照有效期" prop="licensePeriod" style="display: none;">
        <el-input
          v-model="queryParams.licensePeriod"
          placeholder="请输入营业执照有效期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人姓名" prop="corporationName">
        <el-input
          v-model="queryParams.corporationName"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人身份证号码" prop="corporationIdc" style="display: none;">
        <el-input
          v-model="queryParams.corporationIdc"
          placeholder="请输入法人身份证号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人手机号" prop="corporationTel" label-width="100px">
        <el-input
          v-model="queryParams.corporationTel"
          placeholder="请输入法人手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="relationName"  style="display: none;">
        <el-input
          v-model="queryParams.relationName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人电话" prop="relationTel"  style="display: none;">
        <el-input
          v-model="queryParams.relationTel"
          placeholder="请输入联系人电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
     
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:company:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="社会信用代码" align="center" prop="creditCode" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="公司性质" align="center" prop="companyNature">
              <template #default="scope">
                <dict-tag :options="company_nature" :value="scope.row.companyNature"/>
              </template>
            </el-table-column>
            <el-table-column label="机构类型" align="center" prop="types">
              <template #default="scope">
                <dict-tag :options="org_type" :value="scope.row.types"/>
              </template>
            </el-table-column>
     
      <el-table-column label="法人姓名" align="center" prop="corporationName" />
      <el-table-column label="法人身份证号码" align="center" prop="corporationIdc" />
      <el-table-column label="法人手机号" align="center" prop="corporationTel" />
      <el-table-column label="联系人" align="center" prop="relationName" />
      <el-table-column label="联系人电话" align="center" prop="relationTel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:company:edit']">修改</el-button>
		  <el-tooltip content="开通商户" placement="top" v-if="scope.row.tenantId ==null">
         <el-button link type="primary" icon="Edit" @click="handleOpen(scope.row)" v-hasPermi="['system:company:edit']">开通商户</el-button>
		 </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  
 <!-- 开通对话框 -->
   <el-dialog :title="title" v-model="open" width="500px" append-to-body>
     <el-form ref="tenantRef" :model="form" :rules="rules" label-width="80px">
       <el-form-item label="公司名称" prop="companyName">
         <el-input v-model="form.companyName" placeholder="公司名称" readonly/>
       </el-form-item>
       <el-col :span="12">
          <el-form-item label="归属产品" prop="productId">
             <el-select v-model="form.productId" placeholder="请选择产品">
                <el-option
                   v-for="item in productOptions"
                   :key="item.id"
                   :label="item.productName"
                   :value="item.id"
                ></el-option>
             </el-select>
          </el-form-item>
       </el-col>
     </el-form>
     <template #footer>
       <div class="dialog-footer">
         <el-button type="primary" @click="submitForm">确 定</el-button>
         <el-button @click="cancel">取 消</el-button>
       </div>
     </template>
   </el-dialog>
 </div>
</template>

<script setup name="Company">
import { listProduct} from "@/api/system/product";		
import { listCompany, getCompany, delCompany, addCompany, updateCompany,openTenant } from "@/api/system/company";

const { proxy } = getCurrentInstance();
const { org_type, sys_notice_status, company_nature } = proxy.useDict('org_type', 'sys_notice_status', 'company_nature');

const companyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const productOptions= ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyName: null,
    creditCode: null,
    companyNature: null,
    licensePeriod: null,
    corporationName: null,
    corporationIdc: null,
    corporationTel: null,
    relationName: null,
    relationTel: null,
  },
  rules: {
   
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公司信息列表 */
function getList() {
  loading.value = true;
  listCompany(queryParams.value).then(response => {
    companyList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
function getProductSelect() {
  listProduct().then(response => {
    productOptions.value = response.rows;
  });
};

// 表单重置
function reset() {
  form.value = {
    userId: null,
    productId: 1,
  };
  proxy.resetForm("tenantRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}



function handleEdit(row) {
  reset();
  const _userId = row.userId
  getCompany(_userId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改公司信息";
  });
}

/** 修改按钮操作 */
function handleOpen(row) {
  reset();
 getProductSelect();
  const _userId = row.userId
  getCompany(_userId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "开通商户";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tenantRef"].validate(valid => {
    if (valid) {
		openTenant(form.value).then(response => {
		  proxy.$modal.msgSuccess("开通成功");
		  open.value = false;
		  getList();
		});
      // if (form.value.userId != null) {
      //   updateCompany(form.value).then(response => {
      //     proxy.$modal.msgSuccess("修改成功");
      //     open.value = false;
      //     getList();
      //   });
      // } else {
      //   addCompany(form.value).then(response => {
      //     proxy.$modal.msgSuccess("新增成功");
      //     open.value = false;
      //     getList();
      //   });
      // }
    }
  });
}



getList();
</script>
